package com.xframework.security.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Formatter;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class DigestUtil {
    private static final String MD_ALGORITHM = "SHA-1";
    private static final String KEY_ALGORITHM = "AES";
    private static final String INS_ALGORITHM = "AES";

    /**
     * AES加密 SHA-1摘要
     *
     * @param content  需要做摘要的内容
     * @param password 加密密钥
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String digest(String password, String content) throws Exception {
        byte[] key = password.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(INS_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8.name()));
        MessageDigest sha = MessageDigest.getInstance(MD_ALGORITHM);
        encrypted = sha.digest(encrypted);
        return Base64.encodeBase64String(encrypted);
    }

    /**
     * 验证摘要
     *
     * @param digestStr 摘要
     * @param content   需要做摘要的内容
     * @param password  加密密钥
     * @return
     * @throws UnsupportedEncodingException
     */
    public static boolean verify(String digestStr, String password, String content) {
        System.out.println("===============content===============");
        System.out.println(content);
        System.out.println("===============digestStr===============");
        System.out.println(digestStr);
        try {
            String digest = digest(password, content);
            System.out.println("===============digest===============");
            System.out.println(digest);
            return digestStr.equals(digest);
        } catch (Exception e) {
//            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
        return false;
    }

    public static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    public static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }

    public static void main(String[] args) throws Exception {
        String password = "1234567812345678";
        String content = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
        String encode = digest(password, content);
        System.out.println(encode);
        System.out.println(verify(encode, password, content));

    }
}